#!/bin/sh

scriptDir=`cd $(dirname $0);pwd`
# 数据库用户密码，需要先授权anemometer用户，然后再用脚本
host=127.0.0.1
port=3306
username=anemometer
password=anemometer

mysql -h${host} -P${port} -u${username} -p${password} -e "show databases" || (echo "Please grants privileges for ${username} user!" && exit 1)

DirAnemometer=/opt/Anemometer
# 指定具体的域名或ip，可以让外部访问
domainname=127.0.0.1
# php-fpm启动端口
serverPort=9000

# install nginx
source ../00_nginx/install.sh

# install php
release=$(echo `uname -r`|awk -F '.' '{print $4}')
yum install epel-release -y
case ${release} in
  "el7")
    yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm -y
    ;;
  "el6")
    yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm -y
    # rpm -Uvh http://download.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    # yum install --enablerepo=remi,remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common -y
    ;;
  "*")
    echo "not support ${release}, please check it!"
    ;;
esac

yum install --enablerepo=remi,remi-php70 php php-opcache php-dba php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysql php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common -y
sed -i 's:;date.timezone =:date.timezone = Asia/Shanghai:g' /etc/php.ini
php -v

[ -f /etc/php-fpm.d/www.conf ] || exit 1
if [ -f /etc/php-fpm.d/www.conf.bak ];then
  [ -f /etc/php-fpm.d/www.conf ] || rm -f /etc/php-fpm.d/www.conf
  cp /etc/php-fpm.d/www.conf.bak /etc/php-fpm.d/www.conf
else
  cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak
fi
# php-fpm配置使用nginx代理，不使用apache
sed -i 's/^user = .*/user = nginx/g'
sed -i 's/^group = .*/group = nginx/g'
sed -i "s/^listen = .*/listen = 127.0.0.1:${serverPort}/g"
sed -i "s/^listen.acl_users = .*/listen.acl_users = apache,nginx/g"


# install Anemometer
git clone https://github.com/box/Anemometer.git ${DirAnemometer}
[ -f ${DirAnemometer}/conf/config.inc.php ] || rm -f ${DirAnemometer}/conf/config.inc.php
cp ${DirAnemometer}/conf/sample.config.inc.php ${DirAnemometer}/conf/config.inc.php
sed -i "s/\$conn['user'] =/\$conn['user'] =\'${username}\'/g" ${DirAnemometer}/conf/config.inc.php
sed -i "s/\$conn['password'] =/\$conn['password'] =\'${password}\'/g" ${DirAnemometer}/conf/config.inc.php

# 导入sql
mysql -h${host} -P${port} -u${username} -p${password} < ${DirAnemometer}/install.sql || (echo "Please grants sufficient privileges for ${username}" && exit 1)

# 设置nginx配置
[ -f /etc/nginx/conf.d/anemometer.conf ] || rm -f /etc/nginx/conf.d/anemometer.conf
cp ${scriptDir}/anemometer.conf /etc/nginx/conf.d/anemometer.conf
sed -i "s/{{anemometerDir}}/${anemometerDir}/g" /etc/nginx/conf.d/anemometer.conf
sed -i "s/{{domainname}}/${domainmame}/g" /etc/nginx/conf.d/anemometer.conf
sed -i "s/{{port}}/${serverPort}/g" /etc/nginx/conf.d/anemometer.conf

nginx -t || (cat /etc/nginx/conf.d/anemometer.conf && rm -f /etc/nginx/conf.d/anemometer.conf && exit 1)
nginx -s reload

# 启动 php-fpm
service php-fpm start
# service php-fpm stop
# ps -ef|grep php-fpm|grep -v grep|awk '{print $2}'|xargs kill -9